import db from '../db/index.js'

// 使用匿名函数
// 使用 ES6 的按需导出语法,将 getAllUser 方法导出去
// export async function getAllUser(req, res) {
//   // 返回Promise对象
//   //const result=db.query('select id,username,nickname from ev_users')

//   try {
//     // 数组解构出第一个数组赋值给rows
//     const [rows] = await db.query('select id,username,nickname from ev_users')
//     //console.log(rows)
//     res.send({
//       status: 0,
//       message: '获取用户列表数据成功！',
//       data: rows,
//     })
//   } catch (err) {
//     res.send({
//       status: 1,
//       message:'获取用户列表数据失败!',
//       desc:err.message
//     })
//   }
// }

// 使用箭头函数
// 使用 ES6 的按需导出语法,将 getAllUser 方法导出去
export const getAllUser = async (req, res) => {
  // 返回Promise对象
  //const result=db.query('select id,username,nickname from ev_users')

  try {
    // 数组解构出第一个数组赋值给rows
    const [rows] = await db.query('select id,username,nickname from ev_users')
    //console.log(rows)
    res.send({
      status: 0,
      message: '获取用户列表数据成功！',
      data: rows,
    })
  } catch (err) {
    res.send({
      status: 1,
      message: '获取用户列表数据失败!',
      desc: err.message
    })
  }
}

// 测试
//getAllUser()

// 未解构钱返回结果如下:
/*
[
  [
    TextRow { id: 1, username: 'zhangsan', nickname: '张三' },
    TextRow { id: 2, username: 'lisi', nickname: '李四' }
  ],
  [
    ColumnDefinition {
      _buf: <Buffer 01 00 00 01 03 32 00 00 02 03 64 65 66 08 6d 79 5f 64 62 5f 30 31 08 65 76 5f 75 73 65 72 73 08 65 76 5f 75 73 65 72 73 02 69 64 02 69 64 0c 3f 00 0b ... 199 more bytes>,
      _clientEncoding: 'utf8',
      _catalogLength: 3,
      _catalogStart: 10,
      _schemaLength: 8,
      _schemaStart: 14,
      _tableLength: 8,
      _tableStart: 23,
      _orgTableLength: 8,
      _orgTableStart: 32,
      _orgNameLength: 2,
      _orgNameStart: 44,
      characterSet: 63,
      encoding: 'binary',
      name: 'id',
      columnLength: 11,
      columnType: 3,
      flags: 16899,
      decimals: 0
    },
    ColumnDefinition {
      _buf: <Buffer 01 00 00 01 03 32 00 00 02 03 64 65 66 08 6d 79 5f 64 62 5f 30 31 08 65 76 5f 75 73 65 72 73 08 65 76 5f 75 73 65 72 73 02 69 64 02 69 64 0c 3f 00 0b ... 199 more bytes>,
      _clientEncoding: 'utf8',
      _catalogLength: 3,
      _catalogStart: 64,
      _schemaLength: 8,
      _schemaStart: 68,
      _tableLength: 8,
      _tableStart: 77,
      _orgTableLength: 8,
      _orgTableStart: 86,
      _orgNameLength: 8,
      _orgNameStart: 104,
      characterSet: 224,
      encoding: 'utf8',
      name: 'username',
      columnLength: 200,
      columnType: 253,
      flags: 0,
      decimals: 0
    },
    ColumnDefinition {
      _buf: <Buffer 01 00 00 01 03 32 00 00 02 03 64 65 66 08 6d 79 5f 64 62 5f 30 31 08 65 76 5f 75 73 65 72 73 08 65 76 5f 75 73 65 72 73 02 69 64 02 69 64 0c 3f 00 0b ... 199 more bytes>,
      _clientEncoding: 'utf8',
      _catalogLength: 3,
      _catalogStart: 130,
      _schemaLength: 8,
      _schemaStart: 134,
      _tableLength: 8,
      _tableStart: 143,
      _orgTableLength: 8,
      _orgTableStart: 152,
      _orgNameLength: 8,
      _orgNameStart: 170,
      characterSet: 224,
      encoding: 'utf8',
      name: 'nickname',
      columnLength: 200,
      columnType: 253,
      flags: 0,
      decimals: 0
    }
  ]
]
*/